package com.baylor.server.business.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baylor.server.base.util.DynamicSpecifications;
import com.baylor.server.base.util.SearchFilter;
import com.baylor.server.base.util.XaUtil;
import com.baylor.server.base.util.SearchFilter.Operator;
import com.baylor.server.business.entity.XiangAo;
import com.baylor.server.business.repository.XiangAoRespository;
import com.baylor.server.business.service.XiangAoService;


/**
 * 
 * @ClassName: XiangAoServiceImpl
 * @Description: 测试service接口实现
 * @author 曹文波
 * @date 2014年2月24日 下午12:25:07
 * 
 */
@Service("XiangAoService")
@Transactional(readOnly = true)
public class XiangAoServiceImpl implements XiangAoService {

	@Autowired
	private XiangAoRespository xiangAoRespository;

	private static int STATUS_NORMAL = 0;// 正式状态
	private static int STATUS_DELETE = 1;// 删除状态

	public Page<XiangAo> findTestData(Pageable pageable) {
		// 不显示状态为删除的项
		return xiangAoRespository.findByStatusNot(STATUS_DELETE, pageable);
	}

	public Page<XiangAo> findTestDataByFilter(Map<String, Object> filterParams,
			Pageable pageable) {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		// 不显示状态为删除的项
		filters.put("status", new SearchFilter("status", Operator.NE,
				STATUS_DELETE));
		return xiangAoRespository.findAll(DynamicSpecifications.bySearchFilter(
				filters.values(), XiangAo.class), pageable);
	}

	public XiangAo findDataById(Long xiangAoId) {
		return xiangAoRespository.findOne(xiangAoId);
	}


	@Transactional(readOnly = false)
	public XiangAo updateData(XiangAo xiangAo) {
		XiangAo dbItem = null;
		List<XiangAo> dbList = xiangAoRespository.findByXiangAoIdAndStatusNot(
				xiangAo.getXiangAoId(), STATUS_DELETE);
		if (XaUtil.isNotEmpty(dbList)) {
			dbItem = dbList.get(0);
			if (XaUtil.isNotEmpty(dbItem)) {
				dbItem.setName(xiangAo.getName());
				Date editTime = new Date();
				dbItem.setEditTime(editTime);
				xiangAoRespository.save(dbItem);
			}
		}
		return dbItem;
	}


	@Transactional(readOnly = false)
	public XiangAo createData(XiangAo xiangAo) {

		xiangAo.setStatus(STATUS_NORMAL);
		Date editTime = new Date();
		xiangAo.setEditTime(editTime);
		return xiangAoRespository.save(xiangAo);
	}

	@Transactional(readOnly = false)
	public XiangAo delData(XiangAo xiangAo) {
		XiangAo dbItem = null;
		List<XiangAo> dbList = xiangAoRespository.findByXiangAoIdAndStatusNot(
				xiangAo.getXiangAoId(), STATUS_DELETE);
		if (XaUtil.isNotEmpty(dbList)) {
			dbItem = dbList.get(0);
			if (XaUtil.isNotEmpty(dbItem)) {
				dbItem.setStatus(STATUS_DELETE);
				Date editTime = new Date();
				dbItem.setEditTime(editTime);
				xiangAoRespository.save(dbItem);
			}
		}
		return dbItem;
	}

}
